@echo off
chcp 65001 >nul 2>&1
echo ==========================================
echo        API网关容错机制测试
echo ==========================================
echo.
echo 测试基于API7.ai的10种API弹性设计模式
echo 包括：熔断器、限流、重试、超时、降级等
echo.
pause

echo [1] 基础连接测试...
echo -----------------------------------------
echo 测试Gateway服务是否启动:
curl -s -w "Status: %%{http_code}\n" http://localhost:9000/test/ping
if errorlevel 1 (
    echo ❌ Gateway未启动，请先启动Gateway服务
    pause
    exit /b 1
) else (
    echo ✅ Gateway服务正常
)
echo.

echo [2] 容错机制监控端点测试...
echo -----------------------------------------
echo 检查熔断器状态:
curl -s http://localhost:9000/resilience/circuit-breakers | findstr /C:"totalCount" /C:"service"
echo.

echo 检查系统弹性概览:
curl -s http://localhost:9000/resilience/overview | findstr /C:"operational" /C:"resilienceFeatures"
echo.

echo [3] 限流功能测试 (Rate Limiting)...
echo -----------------------------------------
echo 发送连续请求测试限流机制:
for /L %%i in (1,1,15) do (
    echo 第%%i次请求:
    curl -s -w "Status: %%{http_code}" http://localhost:9000/provider/user/getUserById/1 | findstr "Status"
    timeout /t 1 >nul
)
echo.
echo 注意：当请求超过限流阈值时，应该看到429状态码

echo [4] 熔断器功能测试 (Circuit Breaker)...
echo -----------------------------------------
echo 测试正常请求（应该成功）:
curl -s -w "Status: %%{http_code}\n" http://localhost:9000/provider/user/getUserById/1
echo.

echo 测试熔断器状态:
curl -s http://localhost:9000/resilience/circuit-breaker/provider-service-cb | findstr /C:"state" /C:"statusDescription"
echo.

echo [5] 超时机制测试 (Timeout)...
echo -----------------------------------------
echo 测试超时配置（5秒超时限制）:
echo 如果Provider服务响应慢，会触发超时保护
curl -s -w "Status: %%{http_code}, Time: %%{time_total}s\n" http://localhost:9000/provider/user/getUserById/1
echo.

echo [6] 重试机制测试 (Retry)...
echo -----------------------------------------
echo 测试重试功能（最多3次重试）:
echo 当服务返回5xx错误时会自动重试
curl -s -w "Status: %%{http_code}\n" http://localhost:9000/provider/user/getUserById/999
echo.

echo [7] 降级机制测试 (Fallback)...
echo -----------------------------------------
echo 测试熔断器降级响应:
curl -s http://localhost:9000/fallback/provider
echo.
echo.

echo 测试限流降级响应:
curl -s http://localhost:9000/fallback/rate-limit
echo.
echo.

echo [8] VIP用户限流测试...
echo -----------------------------------------
echo 测试VIP用户更宽松的限流策略:
curl -s -w "Status: %%{http_code}\n" -H "X-User-Type: VIP" http://localhost:9000/vip/user/getUserById/1
echo.

echo [9] 负载均衡容错测试...
echo -----------------------------------------
echo 测试负载均衡器的容错能力:
for /L %%i in (1,1,5) do (
    echo 第%%i次负载均衡请求:
    curl -s http://localhost:9000/lb-demo/user/instance-info | findstr "port"
)
echo.

echo [10] 健康检查测试...
echo -----------------------------------------
echo 检查Gateway健康状态:
curl -s http://localhost:9000/actuator/health | findstr /C:"status" /C:"UP"
echo.

echo 检查容错机制健康状态:
curl -s http://localhost:9000/resilience/health | findstr /C:"status" /C:"resilience"
echo.

echo [11] 压力测试模拟...
echo -----------------------------------------
echo 模拟高并发请求，观察容错机制表现:
echo 发送30个并发请求，观察限流和熔断器行为...
for /L %%i in (1,1,30) do (
    start /B curl -s http://localhost:9000/provider/user/getUserById/%%i >nul 2>&1
)
echo 并发请求已发送，等待处理...
timeout /t 5 >nul
echo.

echo 检查压力测试后的熔断器状态:
curl -s http://localhost:9000/resilience/overview | findstr /C:"healthStatus" /C:"total"
echo.

echo [12] Actuator监控数据...
echo -----------------------------------------
echo 检查Prometheus监控指标:
curl -s http://localhost:9000/actuator/prometheus | findstr /C:"resilience4j" /C:"circuit_breaker" | head -5
echo.

echo 检查Gateway路由信息:
curl -s http://localhost:9000/actuator/gateway/routes | findstr /C:"route_id" /C:"CircuitBreaker" | head -3
echo.

echo ==========================================
echo            容错机制测试总结
echo ==========================================
echo.
echo 本测试涵盖了以下API弹性设计模式:
echo ✅ 1. Rate Limiting (限流) - 令牌桶算法
echo ✅ 2. Circuit Breaker (熔断器) - 失败快速响应
echo ✅ 3. Timeout (超时) - 防止长时间等待
echo ✅ 4. Retry (重试) - 自动重试机制
echo ✅ 5. Fallback (降级) - 服务降级响应
echo ✅ 6. Load Balancing (负载均衡) - 流量分发
echo ✅ 7. Health Check (健康检查) - 服务健康监控
echo ✅ 8. Monitoring (监控) - 实时状态观察
echo.
echo 如果某些功能未按预期工作，请检查:
echo 1. Redis服务是否正常启动
echo 2. Provider服务是否在端口11000和11001运行
echo 3. 查看Gateway控制台日志获取详细信息
echo.
echo 参考资料:
echo - API7.ai API弹性设计模式
echo - Microservices.io API Gateway模式
echo - Spring Cloud Gateway官方文档
echo ==========================================
pause 